package cn.goodym.controller;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.goodym.exception.CustomException;
import cn.goodym.po.CaseItem;
import cn.goodym.po.Env;
import cn.goodym.po.PagingVO;
import cn.goodym.service.CaseItemService;
import cn.goodym.service.EnvService;

/**
 * 测试环境控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/env")
public class EnvController {

	@Resource
	private EnvService envService;
	
	@Resource
	private CaseItemService caseItemService;

	/**
	 * 显示测试环境列表
	 * @param model
	 * @param page
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/showEnv")
	public String showEnv(Model model, Integer page) throws Exception {

		List<Env> list = null;
		// 页码对象
		PagingVO pagingVO = new PagingVO();
		// 设置总页数
		pagingVO.setTotalCount(envService.getCount());
		if (page == null || page == 0) {
			pagingVO.setToPageNo(1);
			list = envService.findByPaging(1);
		} else {
			pagingVO.setToPageNo(page);
			list = envService.findByPaging(page);
		}
		model.addAttribute("caseEnvList", list);
		model.addAttribute("pagingVO", pagingVO);
		return "env/showEnv";
	}

	/**
	 * 跳转到添加测试环境页
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addEnv", method = { RequestMethod.GET })
	public String addEnvUI(Model model) throws Exception {
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);
		return "env/addEnv";
	}

	/**
	 * 增加测试环境
	 * @param env
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/addEnv", method = { RequestMethod.POST })
	public String addEnv(Env env, Model model) throws Exception {
	
		Date date = new Date();
		env.setCreateTime(date);
		Boolean result = envService.save(env);
		
		if (!result) {
			model.addAttribute("message", "添加失败");
			return "error";
		}
		return "redirect:/env/showEnv";
	}
	
	/**
	 * 删除数据
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/removeEnv")
	public String removeEnv(Integer id) throws Exception {
		if (id == null) {
			return "env/showEnv";
		}
		envService.removeById(id);

		return "redirect:/env/showEnv";
	}
	
	/**
	 * 跳转到编辑页面
	 * @param id
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editEnv", method = { RequestMethod.GET })
	public String editEnvUI(Integer id, Model model) throws Exception {
		if (id == null) {
			return "redirect:/env/showEnv";
		}
		Env env = envService.findById(id);
		
		List<CaseItem> caseItemList = caseItemService.findAll();
		model.addAttribute("caseItemList",caseItemList);
		
		if (env == null) {
			throw new CustomException("未找到该id");
		}
		
		model.addAttribute("caseEnvList", env);
		return "env/editEnv";
	}
	
	/**
	 * 修改编辑数据
	 * @param env
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/editEnv", method = { RequestMethod.POST })
	public String editEnv(Env env) throws Exception {

		Env envs = envService.findById(env.getId());
		env.setCreateTime(envs.getCreateTime());
		Date date = new Date();
		env.setUpdateTime(date);
		envService.updateById(env.getId(), env);

		return "redirect:/env/showEnv";
	}
	
	 /**
	  * 搜索测试项目
	  * @param findByName
	  * @param model
	  * @return
	  * @throws Exception
	  */
   @RequestMapping(value = "/selectEnv", method = {RequestMethod.POST})
   private String selectItem(String name, Model model) throws Exception {
   	
       List<Env> caseEnvList = envService.findByName(name);

       model.addAttribute("caseEnvList", caseEnvList);
       return "env/showEnv";
   }
	

}
